package com.bee.beeAuth.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bee.beeAuth.entity.SysUser;
import com.bee.beeAuth.service.SysUserService;
import com.bee.beeModel.result.Result;
import com.bee.beeModel.result.ResultTool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;



@RestController
@RequestMapping("/beeAuth/sys-user")
@Api(value ="用户表",tags = "用户表")
public class SysUserController {

    @Resource
    private SysUserService sysUserService;

    /**
    * 新增用户表
    * @param sysUser
    * @return
    */
    @ApiOperation(value = "新增用户表", notes = "新增用户表")
    @PostMapping
    public Result<Boolean> save(@RequestBody SysUser sysUser) {
        sysUser.setCreateTime(LocalDateTime.now());
        return ResultTool.success(sysUserService.save(sysUser));
    }
    /**
    * 修改用户表
    * @param sysUser
    * @return
    */
    @ApiOperation(value = "修改用户表", notes = "修改用户表")
    @PostMapping("/edit")
    public Result<Boolean> edit(@RequestBody SysUser sysUser) {
        sysUser.setUpdateTime(LocalDateTime.now());
        return ResultTool.success(sysUserService.updateById(sysUser));
    }
    /**
    * 根据id删除用户表
    * @param id
    * @return
    */
    @ApiOperation(value = "通过id删除用户表", notes = "通过id删除用户表")
    @DeleteMapping("/{id}")
    public Result<Boolean> removeById(@PathVariable String id) {
        return ResultTool.success(sysUserService.removeById(id));
    }
    /**
    * 根据id批量删除用户表
    * @param ids
    * @return
    */
    @ApiOperation(value = "通过id批量删除用户表", notes = "通过id批量删除用户表")
    @PostMapping("/batch")
    public Result<Boolean> removeById(@RequestBody String[] ids) {
    List<String> idsList = Arrays.asList(ids);
        return ResultTool.success(sysUserService.removeByIds(idsList));
    }
    /**
    * 用户表分页查询
    * @param page
    * @param sysUser
    * @return
    */
    @ApiOperation(value = "用户表分页查询", notes = "用户表分页查询")
    @GetMapping("/page")
    public Result<Page<SysUser>> getGenFormConfPage(Page<SysUser> page, SysUser sysUser) {
        return ResultTool.success(sysUserService.page(page, Wrappers.query(sysUser)));
    }

    /**
    * 根据id查询用户表
    * @param id
    * @return
    */
    @ApiOperation(value = "通过id查询用户表", notes = "通过id查询用户表")
    @GetMapping("/{id}")
    @PreAuthorize("hasAnyAuthority('sys:user:findbyid')")
    public Result<SysUser> getById(@PathVariable("id") String id) {
        return ResultTool.success(sysUserService.getById(id));
    }
}